package com.qm.console.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.green.model.v20180509.ImageSyncScanRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteImageRequest;
import com.aliyuncs.vod.model.v20170321.DeleteImageResponse;

import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import lombok.extern.slf4j.Slf4j;

/**
 * //                       _oo0oo_
 * //                      o8888888o
 * //                      88" . "88
 * //                      (| -_- |)
 * //                      0\  =  /0
 * //                    ___/`---'\___
 * //                  .' \\|     |// '.
 * //                 / \\|||  :  |||// \
 * //                / _||||| -:- |||||- \
 * //               |   | \\\  -  /// |   |
 * //               | \_|  ''\---/''  |_/ |
 * //               \  .-\__  '-'  ___/-. /
 * //             ___'. .'  /--.--\  `. .'___
 * //          ."" '<  `.___\_<|>_/___.' >' "".
 * //         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * //         \  \ `_.   \_ __\ /__ _/   .-` /  /
 * //     =====`-.____`.___ \_____/___.-`___.-'=====
 * //                       `=---='
 * //
 * //
 * //     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * //
 * //               佛祖保佑         永无BUG
 * <p>
 * com.qm.console.utils
 * Created on 2020/7/30.
 */
@Component
@Slf4j
public class AliVodUtils {

    private static final String ACCESS_KEY = "LTAI4G4K45f8pvWG1Rz8npPa";

    private static final String KEY_SECRET = "Soj3w4VaedgpQHh50aPhZZj7Ze7swm";

    private DefaultAcsClient CLIENT;

    @PostConstruct
    private void init() {
        String regionId = "cn-shanghai";  // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, ACCESS_KEY, KEY_SECRET);
        CLIENT = new DefaultAcsClient(profile);
    }

    public boolean deleteImage(String videoId) {
        try {
            deleteImage(CLIENT, videoId);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public DeleteImageResponse deleteImage(DefaultAcsClient client, String videoId) throws Exception {
        DeleteImageRequest request = new DeleteImageRequest();
        request.setDeleteImageType("ImageURL");
        String encodeUrl = URLEncoder.encode(videoId, "UTF-8");
        request.setImageURLs(encodeUrl);
        return client.getAcsResponse(request);
    }

    public String getMediaUrl(String playId, String playType) {
        if (playId.contains(".amr")){  // 兼容海外的amr音频格式
            return playId;
        }
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(playId);
        request.setFormats(playType);
        String url = null;

        try {
            GetPlayInfoResponse response = CLIENT.getAcsResponse(request);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            List<String> playInfos = playInfoList.stream().map(GetPlayInfoResponse.PlayInfo::getPlayURL).collect(Collectors.toList());
            if (playInfos != null && playInfos.size() > 0) {
                url = playInfos.get(0);
//                videoPalyVo.setPlayInfo(playInfos.get(0));
            } else {
                throw new Exception();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }
}
